export default class LocalSave {
  static saveString(key: string, str: string) {
    window.localStorage.setItem(key, str)
  }
  static getString(key: string): string | null {
    return window.localStorage.getItem(key)
  }
  static saveJson<T>(key: string, val: T) {
    let str
    if (typeof val === 'string') {
      str = val
    } else {
      str = JSON.stringify(val)
    }
    window.localStorage.setItem(key, str)
  }
  static getJson(key: string) {
    const val: string | null = window.localStorage.getItem(key)
    return val ? JSON.parse(val) : undefined
  }
  static saveSession(key: string, val: string | object) {
    if (typeof val === 'object') {
      window.sessionStorage.setItem(key, JSON.stringify(val))
    } else {
      window.sessionStorage.setItem(key, val)
    }
  }
  static getSession<T>(key: string, obj = false): string | T | null {
    if (obj) {
      const res: string | null = window.sessionStorage.getItem(key)
      if (res) {
        const result: T = JSON.parse(res)
        return result
      } else {
        return null
      }
    } else {
      const res: string | null = window.sessionStorage.getItem(key)
      return res ? res : null
    }
  }
}
